#include "savedatatool.h"
#include <QFile>
#include <QDir>
#include <QTextStream>
#include <QDebug>
#include <QUrl>

SaveDataTool::SaveDataTool(QObject *parent) : QObject(parent)
{

}

///
/// \brief SaveDataTool::appendFile
/// \param path
/// \param datas
///追加内容
void SaveDataTool::appendFile(const QString& path,const QString& datas){
    if(!isFileExist(path)){
         QFileInfo f(path);
        isDirExist(f.dir().path());
        saveFile(path,datas);
        return;
    }

    QFile file(path);
    file.open(QIODevice::ReadWrite | QIODevice::Append |QIODevice::Text );
//    QTextStream ts(&file);
//    ts<<datas;
      file.write(datas.toUtf8());
    file.close();
}
///
/// \brief SaveDataTool::saveFile
/// \param path
/// \param datas
///写入文件
void SaveDataTool::saveFile(const QString& path,const QString& datas){

       QFile file(path);
       if(!file.open(QIODevice::WriteOnly |QIODevice::Text ))
         qDebug()<<  file.errorString();

          file.write(datas.toUtf8());
       file.close();
}

///
/// \brief SaveDataTool::readFile
/// \param path
/// \return
///读取文件
QString SaveDataTool::readFile(const QString& path){
    QFile file(path);
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QByteArray t = file.readAll();
    QString data= QString(t);
    file.close();
    return data;
}


///
/// \brief SaveDataTool::isFileExist
/// \param fullpath
/// \return
///文件是否存在
bool  SaveDataTool::isFileExist(const QString& fullpath){

    QFileInfo info(fullpath);
    return info.exists();
}

///
/// \brief isDirExist
/// \param fullpath
/// \return
///目录不存在时创建目录
bool  SaveDataTool::isDirExist(const QString& fullpath){

      QDir dir(fullpath);
      if(dir.exists())
          return true;
      return  dir.mkpath(fullpath);
}
